package com.wwj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.reggie.common.CustomException;
import com.wwj.reggie.dto.DishDto;
import com.wwj.reggie.entity.Dish;
import com.wwj.reggie.entity.DishFlavor;
import com.wwj.reggie.entity.SetmealDish;
import com.wwj.reggie.mapper.DishMapper;
import com.wwj.reggie.service.DishFlavorService;
import com.wwj.reggie.service.DishService;
import com.wwj.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品并插入对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto); //将基本菜品信息保存到dish表

        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品及其口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询对应的口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);

        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品及其口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表
        this.updateById(dishDto);

        //清理当前菜品对应的口味的数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        //添加修改后的数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    @Transactional
    public void deleteDish(List<Long> ids) {
        //查询菜品状态
        LambdaQueryWrapper<Dish> lqwStatus = new LambdaQueryWrapper<>();
        lqwStatus.in(Dish::getId, ids).eq(Dish::getStatus, 1);

        int countStatus = this.count(lqwStatus);

        if (countStatus > 0){
            throw new CustomException("菜品正在销售，无法删除！！！");
        }

        //查询菜品是否关联套餐
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getDishId, ids);

        int countSetmealDish = setmealDishService.count(lqw);

        if (countSetmealDish > 0){
            throw new CustomException("该菜品被套餐包含，无法删除！！！");
        }

        List<Dish> list = ids.stream().map((item) -> {
            Dish dish = this.getById(item);
            dish.setIsDeleted(1);
            return dish;
        }).collect(Collectors.toList());

        this.updateBatchById(list);
    }
}
